/*
 * @Date: 2022-02-11 16:16:16
 * @LastEditors: tich425
 * @LastEditTime: 2022-02-11 16:18:07
 * @FilePath: \TodoList\initialize\mysql.go
 * @Description: mysql 管理
 */
package initialize

import (
	"log"
	"todo-list/global"
	"todo-list/model"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

/**
 * @description: 初始化数据库
 */
func InitMysql() {
	// 获取 mysql 配置
	mCfg := global.CONFIG.Mysql.Dsn()
	// 连接
	if db, err := gorm.Open(mysql.Open(mCfg)); err != nil {
		log.Fatal("Connect mysql failed: ", err)
	} else {
		// 自动建表
		db.AutoMigrate(&model.User{})
		db.AutoMigrate(&model.Todo{})
		global.MYSQL_DB = db
		log.Print("Connect mysql success: ", mCfg)
	}
}
